<template>
  <div class="detail">
    <div class="top">
      <div class="header1" :style="opacity2" v-show="isShow">
        <span @click="goback" class="iconfont icon-31fanhui1"></span>
        <span class="iconfont icon-31shouye"></span>
      </div>
      <div :style="opacity" class="header2" v-show="!isShow">
        <span @click="goback" class="iconfont icon-31fanhui1"></span>
        <div>商品详情</div>
        <div>商品评价</div>

        <span class="iconfont icon-31shouye"></span>
      </div>
    </div>
    <div class="middle" ref="wrapper">
      <div>
        <Swiper type="fraction" :imgUrl="detailData.imgUrl"> </Swiper>

        <div class="goods">
          <div class="name">{{ detailData.name }}</div>
          <div class="des">{{ detailData.content }}</div>
          <div class="line"></div>
          <div class="price">
            <div>
              <span class="price"
                ><span style="font-size: 4vw">¥</span
                >{{ detailData.price }}</span
              >
              <span class="weight"> / 150g</span>
            </div>
            <div class="yuan">原价:¥179</div>
          </div>
        </div>
        <img
          style="height: 30vw"
          src="@/components/home/images/dhp.jpg"
          alt=""
        />
        <img
          style="height: 30vw"
          src="@/components/home/images/dhp.jpg"
          alt=""
        />
        <img
          style="height: 30vw"
          src="@/components/home/images/dhp.jpg"
          alt=""
        />
        <img
          style="height: 30vw"
          src="@/components/home/images/dhp.jpg"
          alt=""
        />
        <img
          style="height: 30vw"
          src="@/components/home/images/dhp.jpg"
          alt=""
        />
        <img
          style="height: 30vw"
          src="@/components/home/images/dhp.jpg"
          alt=""
        />
      </div>
    </div>

    <footer class="bottom">
      <div class="icon">
        <div class="icons">
          <span class="iconfont icon-kefu"></span>
          <div>客服</div>
        </div>

        <div class="icons" @click="goCart">
          
            <span class="iconfont icon-gouwuchekong"></span>
            <div class="cart">购物车</div>
          
        </div>

        <div class="icons">
          <span class="iconfont icon-shoucang"></span>
          <div>收藏</div>
        </div>
      </div>
      <div class="add" @click="addcartClick">加入购物车</div>
      <div class="buy">立即购买</div>
    </footer>
  </div>
</template>

<script>
import Swiper from "@/components/home/swiper";
import BetterScroll from "better-scroll";
import { detail } from "@/common/api/detail";
import { addcart } from "@/common/api/cart";
import { Toast } from "vant";
import "vant/es/toast/style";
export default {
  data() {
    return {
      isShow: true,
      BetterScroll: "",
      opacity: {},
      opacity2: {},
      detailData: {},
    };
  },
  components: {
    Swiper,
  },
  methods: {
    goCart(){
      this.$router.push({name:'car'})
    },
    goback() {
      window.history.go(-1);
    },
    addcartClick() {
      let id = this.$route.query.id;

      // 加入购物车
      addcart(id, 1).then((res) => {
        if (res.data.success) {
          Toast("添加成功");
        }
      });
    },
  },
  mounted() {
    // 请求商品数据
    detail(this.$route.query.id).then((res) => {
      this.detailData = res.data.data;
    });

    // 数据渲染完毕在生成betterScroll实例
    this.BetterScroll = new BetterScroll(this.$refs.wrapper, {
      probeType: 3,
      bounce: false,
      click: true,
    });

    // 得到右侧滚动的值
    this.BetterScroll.on("scroll", (pos) => {
      let posy = Math.abs(pos.y);
      let opacity = posy / 400;
      let opacity2 = 1 - posy / 400;

      if (opacity > 1) {
        opacity = 1;
      }
      if (opacity2 < 0) {
        opacity2 = 0;
      }

      this.isShow = posy > 200 ? false : true;

      this.opacity = {
        opacity: opacity,
      };
      this.opacity2 = {
        opacity: opacity2,
      };
    });
  },
};
</script>

<style scoped lang='scss'>
.detail {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  .middle {
    flex: 1;
    overflow: hidden;
  }
  .top {
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    z-index: 999;
  }
}
.header2 {
  font-size: 0.3902rem;
  width: 100%;
  height: 1.0732rem;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .iconfont {
    color: black;
    font-size: 0.639rem;
    margin: 4vw;

    padding: 1vw;
    border-radius: 99px;
  }
}
.header1 {
  width: 100%;
  height: 1.0732rem;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .iconfont {
    color: white;
    font-size: 0.639rem;
    margin: 4vw;
    background-color: rgba($color: #000000, $alpha: 0.3);
    padding: 1vw;
    border-radius: 99px;
  }
}

.goods {
  padding: 0.3463rem;
  .name {
    font-size: 0.439rem;
    color: #232326;
  }
  .des {
    font-size: 0.3415rem;
    color: #999;
  }
  .line {
    width: 100%;
    height: 1px;
    background-color: #999;
    margin: 3vw 0;
  }
  .price {
    .price {
      color: #d22531;
      font-size: 0.75rem;
    }
    .weight {
      color: #949494;
      font-size: 0.4rem;
    }
    .yuan {
      color: #949494;
      font-size: 0.4rem;
    }
  }
}
footer {
  width: 100%;
  height: 1.1951rem;
  background-color: white;
  display: flex;

  border-top: 1px solid rgba(0, 0, 0, 0.05);
  .icon {
    flex: 1;
    display: flex;
    .icons {
      .iconfont {
        font-size: 4vw;
      }
      font-size: 0.3659rem;
      border-right: 1px solid rgba(0, 0, 0, 0.05);
      width: 33.3%;

      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }
  .add,
  .buy {
    font-size: 0.3902rem;
    line-height: 1.1951rem;
    color: white;
    padding: 0 6vw;
  }
  .add {
    background-color: #ff9500;
  }
  .buy {
    background-color: #dd2727;
  }
}

::v-deep .swiper-container {
  width: 100vw;
  height: 10.0976rem;
}
::v-deep img {
  height: 100%;
  width: 100%;
}
::v-deep .swiper-pagination-fraction {
  width: 10vw;
  background-color: rgba($color: #000000, $alpha: 0.5);
  left: 8.5854rem;
  font-size: 0.3902rem;

  text-align: center;
  color: white;
}
</style>